/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.ec.product.web.VirtualProductAction
   Module Description   :

   Date Created      : 2009/12/2
   Original Author   : ellen.li
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.ec.product.web;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

import org.apache.commons.lang.xwork.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.yaodian100.core.common.dao.impl.CommonCriteria;
import com.yaodian100.core.common.entity.SimplePager;
import com.yaodian100.ec.product.entity.Product;
import com.yaodian100.ec.product.entity.VirtualProduct;
import com.yaodian100.ec.product.service.ProductService;

/**
 * @author ellen.li
 * 
 */
public class VirtualProductAction extends ActionSupport implements Preparable {

	/**
	 * serial version
	 */
	private static final long serialVersionUID = 900951934182669169L;
	private final Logger logger = LoggerFactory.getLogger(VirtualProductAction.class);
	private ProductService productService;
	private String productId;
	private SimplePager pager;
	private Product product;
	private VirtualProduct virtualProduct;
	private CommonCriteria crit;
	private Long virtualProductId;

	private String nameParam;
	private String amountParam;
	private String startDateParam;
	private String endDateParam;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.opensymphony.xwork2.Preparable#prepare()
	 */

	public void prepare() throws Exception {
		clearErrorsAndMessages();
		if (pager == null) {
			pager = new SimplePager();
		}
		logger.info("prepare ok");

	}

	public String save() throws Exception {
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		virtualProduct.setVirtualProductName(nameParam);
		virtualProduct.setVirtualProductAmount(Integer.valueOf(amountParam));
		if (StringUtils.isNotEmpty(startDateParam)) {
			virtualProduct.setVirtualProductStartdate(format.parse(startDateParam));
		}
		if (StringUtils.isNotEmpty(endDateParam)) {
			virtualProduct.setVirtualProductEnddate(format.parse(endDateParam));
		}
		virtualProduct.setProductId(productId);
		productService.saveVirtualProduct(virtualProduct);

		return list();
	}

	public String list() throws Exception {
		if (StringUtils.isNotBlank(productId)) {
			product = productService.get(productId);
			crit = new CommonCriteria();
			crit.addEq("productId", productId);
			if (productService.getVirtualProductList(0, 1, crit, null).size() > 0) {
				virtualProduct = productService.getVirtualProductList(0, 1, crit, null).get(0);
			} else {
				virtualProduct = new VirtualProduct();
				virtualProductId = null;
			}
			// logger.info("xxxproductService.getVirtualProductList(0, 1, crit, null).size():{}", productService
			// .getVirtualProductList(0, 1, crit, null).size());
		}
		return "list";
	}

	/**
	 * @return the productService
	 */
	public ProductService getProductService() {
		return productService;
	}

	/**
	 * @param productService the productService to set
	 */
	public void setProductService(ProductService productService) {
		this.productService = productService;
	}

	/**
	 * @return the productId
	 */
	public String getProductId() {
		return productId;
	}

	/**
	 * @param productId the productId to set
	 */
	public void setProductId(String productId) {
		this.productId = productId;
	}

	/**
	 * @return the pager
	 */
	public SimplePager getPager() {
		return pager;
	}

	/**
	 * @param pager the pager to set
	 */
	public void setPager(SimplePager pager) {
		this.pager = pager;
	}

	/**
	 * @return the product
	 */
	public Product getProduct() {
		return product;
	}

	/**
	 * @param product the product to set
	 */
	public void setProduct(Product product) {
		this.product = product;
	}

	/**
	 * @return the virtualProduct
	 */
	public VirtualProduct getVirtualProduct() {
		return virtualProduct;
	}

	/**
	 * @param virtualProduct the virtualProduct to set
	 */
	public void setVirtualProduct(VirtualProduct virtualProduct) {
		this.virtualProduct = virtualProduct;
	}

	/**
	 * @return the nameParam
	 */
	public String getNameParam() {
		return nameParam;
	}

	/**
	 * @param nameParam the nameParam to set
	 */
	public void setNameParam(String nameParam) {
		this.nameParam = nameParam;
	}

	/**
	 * @return the amountParam
	 */
	public String getAmountParam() {
		return amountParam;
	}

	/**
	 * @param amountParam the amountParam to set
	 */
	public void setAmountParam(String amountParam) {
		this.amountParam = amountParam;
	}

	/**
	 * @return the startDateParam
	 */
	public String getStartDateParam() {
		return startDateParam;
	}

	/**
	 * @param startDateParam the startDateParam to set
	 */
	public void setStartDateParam(String startDateParam) {
		this.startDateParam = startDateParam;
	}

	/**
	 * @return the endDateParam
	 */
	public String getEndDateParam() {
		return endDateParam;
	}

	/**
	 * @param endDateParam the endDateParam to set
	 */
	public void setEndDateParam(String endDateParam) {
		this.endDateParam = endDateParam;
	}

	/**
	 * @return the virtualProductId
	 */
	public Long getVirtualProductId() {
		return virtualProductId;
	}

	/**
	 * @param virtualProductId the virtualProductId to set
	 */
	public void setVirtualProductId(Long virtualProductId) {
		this.virtualProductId = virtualProductId;
	}

}
